package com.lemon.secondhandplatform.ui.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.lemon.secondhandplatform.BaseApplication;
import com.lemon.secondhandplatform.R;
import com.lemon.secondhandplatform.mode.User;
import com.lemon.secondhandplatform.ui.activity.good.GoodActivity;
import com.lemon.secondhandplatform.ui.util.StatusBarUtils;
import com.lemon.secondhandplatform.ui.util.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.edtLoginUsername)
    public EditText edtLoginUsername;

    @BindView(R.id.edtLoginPassword)
    public EditText edtLoginPassword;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        bindService();
        StatusBarUtils.drawableStatus(this, R.color.colorPrimary);
    }

    /**
     * 登录
     *
     * @param view
     */
    @OnClick(R.id.btnLogin)
    public void login(View view) {
        //获取用户名和密码
        String username = edtLoginUsername.getText().toString();
        String password = edtLoginPassword.getText().toString();

        //登录
        binder.login(username, password, new FindListener<User>() {
            @Override
            public void done(List<User> list, BmobException e) {
                if (e == null) {
                    User user = list.get(0);
                    if (user != null) {
                        BaseApplication.setUser(user);
                        ToastUtils.show("登录成功");
                        Intent intent = new Intent(LoginActivity.this, GoodActivity.class);
                        startActivity(intent);
                        finish();
                    }
                } else {
                    ToastUtils.show("用户名或密码错误");
                }
            }
        });
    }

    /**
     * 注册
     *
     * @param view
     */
    @OnClick(R.id.btnRegister)
    public void register(View view) {
        Intent intent = new Intent(this, RegisterActivity.class);
        startActivity(intent);
    }

}
